package com.starinside.wp.service.impl;


import com.starinside.wp.domain.ChildActivity;
import com.starinside.wp.domain.UserActiveRegistration;
import com.starinside.wp.repository.ChildActivityRepository;
import com.starinside.wp.repository.UserActiveRegistrationRepository;
import com.starinside.wp.repository.UserChildrenRepository;
import com.starinside.wp.service.AppUserService;
import com.starinside.wp.service.ChildrenService;
import com.starinside.wp.service.UserActiveRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * (UserActiveRegistration)表服务实现类
 *
 * @author makejava
 * @since 2025-02-21 15:11:02
 */
@Service("userActiveRegistrationService")
public class UserActiveRegistrationServiceImpl implements UserActiveRegistrationService {
    @Autowired
    UserActiveRegistrationRepository   userActiveRegistrationRepository;
    @Autowired
    ChildActivityRepository childActivityRepository;
    @Autowired
    AppUserService appUserService;
    @Autowired
    ChildrenService childrenService;
    @Autowired
    UserChildrenRepository userChildrenRepository;

    @Override
    public void application(String activeId, String childId, String parentId, String parentMobile, String relation, String name) {
        ChildActivity childActivity = childActivityRepository.findById(activeId).get();
        UserActiveRegistration userActiveRegistration=userActiveRegistrationRepository.findByActivityIdAndChildrenId(activeId, childId);
        if (userActiveRegistration==null){
            userActiveRegistration =new UserActiveRegistration();
        }
        userActiveRegistration.setActivityId(activeId);
        userActiveRegistration.setUserId(parentId);
        userActiveRegistration.setRelation(relation);
        String name1 = appUserService.selectByPhone(parentMobile).getName();
        userActiveRegistration.setUserName(name1);
        userActiveRegistration.setChildrenId(childId);
        userActiveRegistration.setChildrenName(name);
        userActiveRegistration.setPhone(parentMobile);
        userActiveRegistration.setRegisterTime(new Date());
        userActiveRegistration.setAreaId(childActivity.getAreaId());
        userActiveRegistration.setIsActive(Boolean.TRUE);
        userActiveRegistrationRepository.save(userActiveRegistration);


    }

    @Override
    public void calApplication(String activeId, String childId) {
        UserActiveRegistration byActivityIdAndChildrenId = userActiveRegistrationRepository.findByActivityIdAndChildrenId(activeId, childId);
        if (byActivityIdAndChildrenId!=null){
            byActivityIdAndChildrenId.setIsActive(Boolean.FALSE);
            userActiveRegistrationRepository.save(byActivityIdAndChildrenId);
        }
    }
}
